import { Message } from "element-ui";
import APIS from "@/api/index";
export function yearDate() {
  return new Date().getFullYear().toString();
}
export function shangnianyearDate() {
  const currentYear = new Date().getFullYear();
  const lastYear = currentYear - 1;
  return lastYear.toString();
}

export function monthDate() {
  let myDate = new Date();
  let nowY = myDate.getFullYear();
  let nowM = myDate.getMonth() + 1;
  let endDate = nowY + "-" + (nowM < 10 ? "0" + nowM : nowM);
  return endDate;
}
export function timestampToTime() {
  var date = new Date();
  var Y = date.getFullYear() + "-";
  var M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var h =
    (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";

  return Y + M + D;
}
export function timestampToTime1() {
  var date = new Date(); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear();
  var M =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  var M1 = date.getMonth() < 10 ? "0" + date.getMonth() : date.getMonth();
  var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return [`${Y}-${M1}-${D}`, `${Y}-${M}-${D}`];
}

export function getData(day) {
  var today = new Date();
  var targetday = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday);
  var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  tMonth = doHandMonth(tMonth + 1);
  tDate = doHandMonth(tDate);
  return tYear + "-" + tMonth + "-" + tDate;
}
export function doHandMonth(month) {
  var m = month;
  if (month.toString().length == 1) {
    m = "0" + month;
  }
  return m;
}

export function loadCSS(url, isCache = false) {
  let element = document.createElement("link");
  element.setAttribute("rel", "stylesheet");
  element.setAttribute("type", "text/css");
  if (isCache) {
    element.setAttribute("href", url + "?t=" + new Date().getTime());
  } else {
    element.setAttribute("href", url);
  }
  document.head.appendChild(element);
}

export function returnFloat(num) {
  num = (parseInt(num * 100) / 100).toFixed(2);

  // num = num.toString(); // 转成字符串类型  如能确定类型 这步可省去
  // if (num.indexOf(".") !== -1) {
  //   let [integerPart, decimalPart] = num.split(".");

  //   if (decimalPart.length > 2) {
  //     decimalPart = decimalPart.substring(0, 2);
  //   } else if (decimalPart.length === 1) {
  //     decimalPart += "0";
  //   }

  //   num = `${integerPart}.${decimalPart}`;
  // } else {
  //   num += ".00";
  // }

  return num;
}

export function Logout() {
  Message({
    message: "登录过期！",
    offset: window.screen.height / 2.8,
    duration: 1000,
  });
  APIS.Baseapi.loginout()
    .then((res) => {})
    .catch((err) => {
      console.log(err);
    });
  document.cookie = "Token-buyer=; path=/;";
  document.cookie = "Token-usertype=; path=/;";
  document.cookie = "Token-operator=; path=/;";
  document.cookie = "Token-seller=; path=/;";

  localStorage.removeItem("kspcrightldt");
  localStorage.removeItem("statusarr");
  localStorage.removeItem("kspcbottomtopten");

  setTimeout(() => {
    // window.location.reload();
  }, 1500);
}

export function getParameter(){
  let url = window.location.href.split("?")[1];
  var reg = new RegExp("(^|&)" + "popupType" + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  var dd = "";
  if(url){
    var r = url.match(reg); //匹配目标参数
    dd = decodeURIComponent(r[2]);
  }
  return dd
}
